﻿Imports System
Imports System.ComponentModel
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class CustomProperty
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider)
            Me.serviceProvider = serviceProvider
        End Sub

        Public Shared Function CreateCustomProperty(ByVal serviceProvider As IServiceProvider, ByVal customPropertyName As String, ByVal propertyDescriptor As PropertyDescriptor, ByVal propertyOwner As Object) As CustomProperty
            Dim property As New CustomProperty(serviceProvider) { _
                .Name = customPropertyName _
            }
            If TypeProvider.IsAssignable(GetType(ActivityBind), propertyDescriptor.PropertyType) Then
                Dim type As Type = PropertyDescriptorUtils.GetBaseType(propertyDescriptor, propertyOwner, serviceProvider)
                If (type Is Nothing) Then
                    Throw New InvalidOperationException(SR.GetString("Error_CantDeterminePropertyBaseType", New Object() { propertyDescriptor.Name }))
                End If
                [property].Type = type.FullName
            Else
                [property].Type = propertyDescriptor.PropertyType.FullName
            End If
            If TypeOf propertyDescriptor Is ActivityBindPropertyDescriptor Then
                Dim property2 As DependencyProperty = DependencyProperty.FromName(propertyDescriptor.Name, propertyDescriptor.ComponentType)
                [property].IsEvent = ((Not property2 Is Nothing) AndAlso property2.IsEvent)
            End If
            [property].Category = propertyDescriptor.Category
            Return [property]
        End Function


        ' Properties
        Public Property Browseable As Boolean
            Get
                Return Me.browseable
            End Get
            Set(ByVal value As Boolean)
                Me.browseable = value
            End Set
        End Property

        Public Property Category As String
            Get
                Return Me.category
            End Get
            Set(ByVal value As String)
                Me.category = value
            End Set
        End Property

        Public Property Description As String
            Get
                Return Me.description
            End Get
            Set(ByVal value As String)
                Me.description = value
            End Set
        End Property

        Public Property DesignerSerializationVisibility As DesignerSerializationVisibility
            Get
                Return Me.designerSerializationVisibility
            End Get
            Set(ByVal value As DesignerSerializationVisibility)
                Me.designerSerializationVisibility = value
            End Set
        End Property

        Public Property GenerateDependencyProperty As Boolean
            Get
                Return Me.generateDependencyProperty
            End Get
            Set(ByVal value As Boolean)
                Me.generateDependencyProperty = value
            End Set
        End Property

        Public Property Hidden As Boolean
            Get
                Return Me.hidden
            End Get
            Set(ByVal value As Boolean)
                Me.hidden = value
            End Set
        End Property

        Public Property IsEvent As Boolean
            Get
                Return Me.isEvent
            End Get
            Set(ByVal value As Boolean)
                Me.isEvent = value
            End Set
        End Property

        Public Property Name As String
            Get
                Return Me.name
            End Get
            Set(ByVal value As String)
                Me.name = value
            End Set
        End Property

        Public Property Type As String
            Get
                Return Me.type
            End Get
            Set(ByVal value As String)
                Me.type = value
            End Set
        End Property

        Public Property UITypeEditor As String
            Get
                Return Me.uiTypeEditor
            End Get
            Set(ByVal value As String)
                Dim name As String = value
                If (Not Me.serviceProvider Is Nothing) Then
                    Dim service As ITypeProvider = [TryCast](Me.serviceProvider.GetService(GetType(ITypeProvider)),ITypeProvider)
                    If (service Is Nothing) Then
                        Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(ITypeProvider).FullName }))
                    End If
                    Dim type As Type = service.GetType(name)
                    If (Not type Is Nothing) Then
                        name = type.FullName
                    End If
                End If
                Me.uiTypeEditor = name
            End Set
        End Property


        ' Fields
        Private browseable As Boolean = True
        Private category As String
        Private description As String
        Private designerSerializationVisibility As DesignerSerializationVisibility = DesignerSerializationVisibility.Visible
        Private generateDependencyProperty As Boolean = True
        Private hidden As Boolean
        Private isEvent As Boolean
        Private name As String
        Public oldPropertyName As String
        Public oldPropertyType As String
        Private serviceProvider As IServiceProvider
        Private type As String
        Private uiTypeEditor As String
    End Class
End Namespace

